# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

EAPI=7

inherit autotools git-r3 systemd

DESCRIPTION="The USBGuard software framework helps to protect your computer against BadUSB."
HOMEPAGE="https://github.com/dkopecek/usbguard"
MY_AUTHOR="dkopecek"
EGIT_REPO_URI="https://github.com/${MY_AUTHOR}/${PN}.git"
EGIT_SUBMODULES=( src/ThirdParty/Catch src/ThirdParty/PEGTL src/ThirdParty/usbmon )

if [[ ${PV} == "9999" ]] ; then
	EGIT_BRANCH="master"
	KEYWORDS=""
else
	EGIT_COMMIT="usbguard-${PV}"
	KEYWORDS="~x86 ~amd64"
fi

LICENSE="GPL-2"
SLOT="0"
IUSE="crypt +dbus +policykit +systemd"
IUSE+=" crypt_gcrypt crypt_sodium"

DEPEND="sys-cluster/libqb
		sys-libs/libseccomp
		sys-libs/libcap-ng
		dev-ruby/asciidoctor
		dev-libs/protobuf
		>=sys-cluster/libqb-0.16.0
		systemd? ( sys-apps/systemd )
		dbus? ( sys-apps/dbus
						dev-libs/dbus-glib )
		policykit? ( sys-auth/polkit )
		crypt? (
			crypt_gcrypt? ( dev-libs/libgcrypt )
			crypt_sodium? ( dev-libs/libsodium )
		)"

RDEPEND="${DEPEND}
				virtual/udev"

REQUIRED_USE="
		crypt? (
			^^ (
					crypt_sodium
					crypt_gcrypt
			)
		)"
		
#PATCHES=(
#	"${FILESDIR}/openrc-support.patch"
#)
		

src_prepare() {
	default
	eautoreconf
	#eapply "${WORKDIR}"/openrc-support.patch
}

src_configure() {
	local myconf=()

	if use crypt_gcrypt; then
		myconf+=( --with-crypto-library=gcrypt )
 	elif use crypt_sodium; then
		myconf+=( --with-crypto-library=sodium )
	fi

	if use policykit; then
		myconf+=( --with-polkit )
	fi

	econf \
		$(use_enable systemd) \
		$(use_with dbus) \
		--with-bundled-catch \
		--with-bundled-pegtl \
		"${myconf[@]}"
		
    
}

src_install() {
	emake DESTDIR="${D}" install

	if use systemd; then
		systemd_dounit usbguard.service
	fi

	doinitd "${FILESDIR}"/openRC/usbguard
	doinitd "${FILESDIR}"/openRC/usbguard-dbus
	doconfd "${FILESDIR}"/openRC/conf.d/usbguard
	doconfd "${FILESDIR}"/openRC/conf.d/usbguard-dbus
	
	insinto "/etc/usbguard"
	doins usbguard-daemon.conf
}

pkg_postinst() {
    elog "You will need to allow access to your user for accessing the QT gui."
    elog "Execute as root. usbguard add-user YOUR_USERNAME --devices ALL --exceptions ALL"
}
